package com.obas.access.business.service.impl;

import java.util.List;

import com.ease.common.vo.MapParameter;
import com.ease.platform.access.service.impl.AbstractNamespaceAccessServiceImpl;
import com.obas.access.business.service.SystemScenarioAccessService;
import com.obas.access.business.service.SystemUsecaseAccessService;
import com.obas.model.business.SystemScenario;
import com.obas.model.business.SystemUsecase;
import com.obas.model.business.criteria.SystemScenarioCriteria;

public class SystemScenarioAccessServiceImpl extends AbstractNamespaceAccessServiceImpl<SystemScenario> implements SystemScenarioAccessService {

	static final class Statement {
		public static final String associateSystemProject = "associateSystemProject";
		public static final String disassociateSystemProject = "disassociateSystemProject";
		public static final String disassociateAllSystemProject = "disassociateAllSystemProject";
		public static final String disassociateAllSystemUsecase = "disassociateAllSystemUsecase";
	}

	private SystemUsecaseAccessService systemUsecaseAccessService;

	public List<SystemScenario> findBySystemProjectId(long systemProjectId) {
		SystemScenarioCriteria parameter = new SystemScenarioCriteria();
		parameter.setSystemProjectId(systemProjectId);
		return this.find(parameter);
	}

	public SystemScenario createEntity(SystemScenario systemScenario, long systemProjectId) {
		systemScenario = this.create(systemScenario);
		if (systemProjectId > 0) {
			associateSystemProject(systemScenario.getId(), systemProjectId);
		}
		return systemScenario;
	}

	public SystemScenario remove(SystemScenario systemScenario) {
		if (null != systemScenario) {

			long systemScenarioId = systemScenario.getId();
			//
			disassociateAll(systemScenarioId, systemScenario);
			//
			systemScenario = super.remove(systemScenario);
			//
			removeCascade(systemScenarioId, systemScenario);
		}
		return systemScenario;
	}

	public void removeCascade(long systemScenarioId, SystemScenario systemScenario) {
		{/* TBD */
			List<SystemUsecase> systemUsecases = systemUsecaseAccessService.findBySystemScenarioId(systemScenarioId);
			if (notEmpty(systemUsecases)) {
				for (SystemUsecase systemUsecase : systemUsecases) {
					systemUsecaseAccessService.remove(systemUsecase);
				}
			}
		}
	}

	public void disassociateAll(long systemScenarioId, SystemScenario systemScenario) {
		disassociateAllSystemProject(systemScenarioId);
		disassociateAllSystemUsecase(systemScenarioId);
	}

	public void disassociateAllSystemProject(long systemScenarioId) {
		MapParameter parameter = new MapParameter();
		parameter.put("systemScenarioId", systemScenarioId);
		this.update(Statement.disassociateAllSystemProject, parameter);
	}

	public void disassociateAllSystemUsecase(long systemScenarioId) {
		MapParameter parameter = new MapParameter();
		parameter.put("systemScenarioId", systemScenarioId);
		this.update(Statement.disassociateAllSystemUsecase, parameter);
	}

	public void associateSystemProject(long systemScenarioId, long systemProjectId) {
		MapParameter parameter = new MapParameter();
		parameter.put("systemProjectId", systemProjectId);
		parameter.put("systemScenarioId", systemScenarioId);
		this.update(Statement.associateSystemProject, parameter);
	}

	public void disassociateSystemProject(long systemScenarioId, long systemProjectId) {
		MapParameter parameter = new MapParameter();
		parameter.put("systemProjectId", systemProjectId);
		parameter.put("systemScenarioId", systemScenarioId);
		this.remove(Statement.disassociateSystemProject, parameter);
	}

	/**
	 * @return the systemUsecaseAccessService
	 */
	public SystemUsecaseAccessService getSystemUsecaseAccessService() {
		return systemUsecaseAccessService;
	}

	/**
	 * @param systemUsecaseAccessService
	 *            the systemUsecaseAccessService to set
	 */
	public void setSystemUsecaseAccessService(SystemUsecaseAccessService systemUsecaseAccessService) {
		this.systemUsecaseAccessService = systemUsecaseAccessService;
	}

}
